package com.SmartClassroom.system.controller;

import com.SmartClassroom.system.annotation.AuthCheck;
import com.SmartClassroom.system.common.BaseResponse;
import com.SmartClassroom.system.common.DeleteRequest;
import com.SmartClassroom.system.common.ErrorCode;
import com.SmartClassroom.system.common.ResultUtils;
import com.SmartClassroom.system.constant.UserConstant;
import com.SmartClassroom.system.exception.BusinessException;
import com.SmartClassroom.system.exception.ThrowUtils;
import com.SmartClassroom.system.model.domain.Course;
import com.SmartClassroom.system.model.domain.User;
import com.SmartClassroom.system.model.dto.course.CourseAddRequest;
import com.SmartClassroom.system.model.dto.course.CourseQueryRequest;
import com.SmartClassroom.system.model.dto.course.CourseUpdateRequest;
import com.SmartClassroom.system.model.vo.CourseVO;
import com.SmartClassroom.system.service.CourseService;
import com.SmartClassroom.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import static com.SmartClassroom.system.common.CourseUtil.setCourseId;
import static com.SmartClassroom.system.common.CourseUtil.setDayOfWeek;

/**
 * 课程表接口
 *
 * @author baijiayu
 */
@RestController
@RequestMapping("/course")
@Slf4j
@Api(tags = { "课程表接口" })
public class CourseController {

    @Resource
    private UserService userService;

    @Resource
    private CourseService courseService;

    // region 增删改查

    /**
     * 创建课程
     *
     * @param courseAddRequest
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "创建课程")
    public BaseResponse<Integer> addCourse(@RequestBody CourseAddRequest courseAddRequest) {
        if (courseAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Course course = new Course();
        BeanUtils.copyProperties(courseAddRequest, course);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userName", courseAddRequest.getTeacherName());
        User user = userService.getOne(queryWrapper);
        ThrowUtils.throwIf(user == null, ErrorCode.NOT_FOUND_ERROR, "教师不存在");
        course.setTeacherId(user.getId());
        setDayOfWeek(course, courseAddRequest.getDayOfWeek());
        setCourseId(course, courseAddRequest.getCourseName());
        boolean result = courseService.save(course);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "创建失败");
        return ResultUtils.success(course.getCourseId());
    }

    /**
     * 删除课程
     *
     * @param deleteRequest
     * @return BaseResponse<Boolean>
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "删除课程")
    public BaseResponse<Boolean> deleteCourse(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 根据id查询课程
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", deleteRequest.getId());
        queryWrapper.eq("isDelete", 0);
        Course course = courseService.getOne(queryWrapper);
        ThrowUtils.throwIf(course == null, ErrorCode.NOT_FOUND_ERROR, "课程不存在");
        // 执行删除操作
        boolean result = courseService.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "删除失败");
        return ResultUtils.success(result);
    }

    /**
     * 更新课程
     *
     * @param courseUpdateRequest
     * @return BaseResponse<Boolean>
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "更新课程")
    public BaseResponse<Boolean> updateCourse(@RequestBody CourseUpdateRequest courseUpdateRequest) {
        if (courseUpdateRequest == null || courseUpdateRequest.getCourseId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Course course = new Course();
        BeanUtils.copyProperties(courseUpdateRequest, course);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userName", courseUpdateRequest.getTeacherName());
        User user = userService.getOne(queryWrapper);
        ThrowUtils.throwIf(user == null, ErrorCode.NOT_FOUND_ERROR, "教师不存在");
        course.setCourseId(courseUpdateRequest.getCourseId());
        course.setTeacherId(user.getId());
        setDayOfWeek(course, courseUpdateRequest.getDayOfWeek());
        boolean result = courseService.updateById(course);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "更新失败");
        return ResultUtils.success(true);
    }

    /**
     * 分页获取全部的课程表信息
     *
     * @param courseQueryRequest
     * @return
     */
    @PostMapping("/get/all")
    @ApiOperation(value = "获取全部的课程表信息")
    public BaseResponse<Page<CourseVO>> getCourse(@RequestBody CourseQueryRequest courseQueryRequest) {
        // 分页参数
        long current = courseQueryRequest.getCurrent() == 0 ? 1 : courseQueryRequest.getCurrent();
        long size = courseQueryRequest.getPageSize() == 0 ? 10 : courseQueryRequest.getPageSize();

        // 分页对象
        Page<CourseVO> pageParam = new Page<>(current, size);
        Page<CourseVO> courseVOPage = courseService.getQueryWrapper(pageParam, courseQueryRequest);

        return ResultUtils.success(courseVOPage);
    }

}
